# 用list实现顺序表
class SeqList(object):
    def __init__(self, max=8):
        self.max = max
        self.num = 0
        self.data = [None] * self.max

    def is_empty(self):
        return self.num == 0

    # 获取某个值,这样类可以进行迭代
    def __getitem__(self, item):
        if not isinstance(item, int):
            raise TypeError
        if 0 <= item < self.num:
            return self.data[item]
        else:
            raise IndexError

    def append(self, value):
        if self.num == self.max:
            print("list is full.")
            return
        else:
            self.data[self.num] = value
            self.num += 1

    def __len__(self):
        return self.num

    def pop(self, key=-1):
        if not isinstance(key, int):
            raise IndexError
        if self.num - 1 < 0:
            raise IndexError("pop error .")
        elif key == -1:
            self.num -= 1
        #     删除某个index的值
        else:
            for i in range(key, self.num - 1):
                self.data[i] = self.data[i + 1]
            self.num -= 1

    #     搜索操作
    def search(self, data, start=0):
        for i in range(start, self.num):
            if self.data[i] == data:
                return True
        return False


if __name__ == "__main__":
    s = SeqList()
    for i in range(10):
        s.append(i)
    #     迭代
    for ik in s:
        print(ik)

    #     搜索
    print(s.search(3))
